axios.defaults.baseURL = 'http://cba.itlike.com/public/index.php?s=/api/'


document.querySelector('.left').addEventListener('click',function () {
  location.href = './loging.html'
})

// 获取本地token
const usertoken = localStorage.getItem('token')
// 登录成功后渲染用户数据
async function render() {
  if (usertoken) {
    const res = await axios.get('/user/info', {
      headers:{
        'Access-Token': usertoken,
        platform:'H5'
      }
    })
    console.log(res)
    const ele = res.data.data.userInfo
    // 推出按钮显示
    document.querySelector('.backlogin').style.display = 'block'
    // 头像和名称
    document.querySelector('.user').innerHTML = `
    <div class="right">
      <img src="${!ele.avatar ?'http://cba.itlike.com/public/mweb/static/default-avatar.png':ele.avatar}" alt="">
    </div>
      <div class="left">
        <div class="nick">${ele.nick_name}</div>
        <div class="login">${ele.mobile}</div>
      </div>
    `
    // 账户余额和积分
    const res2 = await axios.get('/user/assets', {
      headers: {
        'Access-Token': usertoken,
        platform:'H5'
      }
    })
    console.log(res2)
    const item = res2.data.data.assets
    document.querySelector('.balance').innerHTML = `
    <div class="asset-left-item">
        <div class="xian">${item.balance}</div>
        <div class="tx">账户余额</div>
    </div>
    <div class="asset-left-item">
        <div class="xian">${item.points}</div>
        <div class="tx">积分</div>
    </div>
    <div class="asset-left-item">
        <div class="xian">${item.coupon}</div>
        <div class="tx">优惠券</div>
    </div>
    `
    document.querySelector('.backlogin').addEventListener('click',function () {
      localStorage.removeItem('token')
      localStorage.removeItem('userId')
      location.reload(true)
    })
  }
}
render()





//跳转页面
const li = document.querySelectorAll('.footer li')
for (let i = 0; i < li.length; i++) {
  li[i].addEventListener('click', function () {
    document.querySelector('.footer li.active').classList.remove('active')
    li[i].classList.add('active')
  })
}
li[0].addEventListener('click', function () {
  location.href = 'index.html'
})
li[1].addEventListener('click', function () {
  location.href = 'classify.html'
})
li[2].addEventListener('click',function () {
  location.href = 'shopcar.html'
})
li[3].addEventListener('click',function () {
  location.href = 'register.html'
})
